﻿using CKS.Core.Framework;
using Microsoft.Extensions.DependencyInjection;
using Infrastructure;

using Microsoft.Extensions.Logging;

using ZR.Model.Models.ProductionControl;
using CKS.PLC.Action.Subscription.OP10;

namespace CKS.PLC.Action.OP10
{
    /// <summary>
    /// 
    /// 出清洗站 - 清洗线数量做减法 - 增加缓存线数量
    /// 
    /// </summary>
    public class Cks102Action : LoadingActionBase
    {
        public Cks102Action(DecodeValue _value) : base(_value)
        {
        }

        public override void Execute()
        {
            var replayDB = 3010;
            var replayAction = 102;
            var result = Value.Value.ToInt();
            var replayPos = Value.GetPosition(replayDB, replayAction);
            if (result == 1)
            {

                string bluecode = Value.GetLinkData(1).ToString();
              
                if (CheckCodeIsError(bluecode))
                {

                    AckWriteInt16(replayPos, 1);
                    EventBus.AddLog($"OP10-出清洗线  {bluecode}");
                    return;
                }
                /*
                 * 根据子篮查询零件类型
                 */

                var PartType = GetPartType(bluecode);


                var config = db.Queryable<CksLoadingplanConfig>()
                    .Where(a => a.PartType == PartType).First();
                // config.BasketCapacity
                config.LastLoadingTime = DateTime.Now;
                db.Updateable(config).UpdateColumns(a => new { a.LastLoadingTime }).ExecuteCommand();

                var records = db.Queryable<CksBasketcleaningRecord>()
                    .Where(a => a.BlueCode == bluecode && a.Status == 1).OrderByDescending(a => a.Id)
                    .ToList();
                
                //更新子篮表的数量，如果子篮零件数量不是0，需要修正一下缓存线零件数量
                var basket = db.Queryable<CksBasket>().Where(a => a.BlueCode == bluecode).First();
                if (basket == null)
                {
                    basket = new CksBasket() { BlueCode = bluecode, CreatedTime = DateTime.Now, Type = PartType };
                    basket.Stock = config.BasketCapacity;
                    basket.Status = 2;
                    db.Insertable(basket).ExecuteCommand();
                }
                else
                {
                    basket.Status = 2;
                    basket.Stock = config.BasketCapacity;
                    basket.UpdateTime = DateTime.Now;
                    db.Updateable(basket).UpdateColumns(a => new { a.Stock, a.UpdateTime,a.Status }).ExecuteCommand();
                }

                //更新出清洗线的时间
                
                if (records.Count>0)
                {
                    foreach (var record in records)
                    {
                        record.Status = 10;
                        record.OutTime = DateTime.Now;
                        db.Updateable(record).UpdateColumns(a => new { a.Status, a.OutTime }).ExecuteCommand();
                    }
                   
                }

                PushPartCount(PartType);
               
                AckWriteInt16(replayPos, 1);
                EventBus.AddLog($"OP10-出清洗线 {bluecode} ");
            }
            else
            {
                
                AckWriteInt16(replayPos, 0);
                EventBus.AddLog($"OP10-出清洗线 重置状态");
            }
        }

    }
}
